//
//  Extension+UIImage.swift
//  LKWeibo
//
//  Created by like on 16/8/18.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

extension UIImage{
    
    // 自定义表情键盘中获取image
    class func emoticonsImage(emoticonModel: LKEmoticonModel) -> UIImage?{
        let png = emoticonModel.png ?? ""
        let path = emoticonModel.path ?? ""
        let image = UIImage(named: "\(path)\(png)", inBundle: LKEmoticonTools.sharedTools.emoticonsBundle, compatibleWithTraitCollection: nil)
        return image
    }
    

    
    class func dealImageScale(image: UIImage, width: CGFloat) -> UIImage{
        
        //如果你想压缩的图片 本身就很小了  不需要压缩了  直接return
        if image.size.width < width {
            return image
        }
        
        let height = image.size.height*width/image.size.width
        // 等比例后的高度
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        // 01 开启上下文
        UIGraphicsBeginImageContext(CGSize(width: width, height: height))
        // 02 把内容渲染到上下文中
        image.drawInRect(rect)
        // 03 从上下文获取内容
        let result = UIGraphicsGetImageFromCurrentImageContext()
        // 04 关闭上下文
        UIGraphicsEndImageContext()
        return result
    }
    
    
}